unit TPS_TambahN;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ZAbstractTable, ZDataset, DB, ZAbstractRODataset,
  ZAbstractDataset, ZConnection, Setting, ExtCtrls;

type
  TForm_TambahTPS = class(TForm)
    lblTPS: TLabel;
    lblKet: TLabel;
    Label1: TLabel;
    txtJml: TEdit;
    btnTambah: TButton;
    Button1: TButton;
    lblTambah: TLabel;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    Panel1: TPanel;
    Label2: TLabel;
    txtSatuan: TEdit;
    procedure btnTambahClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure txtJmlChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure txtJmlKeyPress(Sender: TObject; var Key: Char);
    procedure txtSatuanKeyPress(Sender: TObject; var Key: Char);
  private
    function cekNamaTPS(kata: String): String;
    procedure KeyOnlyNum(Sender: TObject; var Key: Char);
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; desa: String; mode:integer);             overload;
  end;

var
  Form_TambahTPS: TForm_TambahTPS;
  urut:Integer;
  sdesa:String;
  smode:integer;

implementation

{$R *.dfm}

constructor TForm_TambahTPS.Create(AOwner: TComponent; desa: String; mode:integer);
begin
  smode:=mode;
  sdesa:=desa;
  inherited Create(AOwner);
end;

procedure TForm_TambahTPS.btnTambahClick(Sender: TObject);
var
  i,ntp:Integer;
  buttonSelected : Integer;
  akan : string;
  boleh : boolean;
begin
  if (smode = 1) then begin
  buttonSelected := MessageDlg('Apakah anda yakin akan menambahkan '+txtJml.Text+' TPS?',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
    for I := 1 to strtoint(txtJml.Text) do begin
      ntp:=urut+i;
      with ZQuery1 do  //masukkin nih tps gan
      begin
        Active:=False;
        sql.Clear;
        sql.Text := 'INSERT INTO daftar_tps (NAMA, KODE_DESA, ALAMAT) VALUES ("'+cekNamaTPS(IntToStr(ntp))+'","'+sdesa+'","")';
        ExecSQL;
      end;
    end;
    ShowMessage('Sudah menambahkan sebanyak '+txtJml.Text+' TPS');
    Close;
    end;
  end
  else begin
    if(txtSatuan.Text <> '') then begin
    boleh := true;
    akan := cekNamaTPS(txtSatuan.Text);
    buttonSelected := MessageDlg('Apakah anda yakin akan menambahkan TPS '+akan+'?',mtConfirmation , mbOKCancel, 0);
    if buttonSelected = mrOK  then begin
      //cek dulu
      with ZQuery1 do
      begin
        Active:=False;
        sql.Clear;
        sql.Text := 'SELECT * FROM daftar_tps WHERE KODE_DESA = '+quotedstr(sdesa)+' AND NAMA = '+quotedstr(akan);
        active:=true;
        if RecordCount > 0 then boleh := false;
      end;

      if boleh=true then begin
        //masukin TPS nya
        with ZQuery1 do
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'INSERT INTO daftar_tps (NAMA, KODE_DESA, ALAMAT) VALUES ("'+akan+'","'+sdesa+'","")';
          ExecSQL;
        end;
        ShowMessage('Berhasil menambahkan TPS '+akan);
        Close;
      end
      else
        showMessage('TPS yang akan diisikan sudah ada, silakan periksa kembali.');
    end;
    end else
      showmessage('TPS tidak boleh kosong');
  end;
end;

procedure TForm_TambahTPS.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm_TambahTPS.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TForm_TambahTPS.FormCreate(Sender: TObject);
var
  setting:TSetting_Form;
  ntps,ikabkot,ikec,idesa:String;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password := setting.password;
  ZConnection1.LoginPrompt := False;
  ZConnection1.Connected := True;
  ZQuery1.Connection:=ZConnection1;
  ZTable1.Connection:=ZConnection1;
  BorderIcons := BorderIcons - [biMaximize];
  //Keterangan
  with zquery1 do begin
    active:=false;
    sql.Clear;
    sql.Text:='SELECT * FROM desa WHERE KODE_DESA='+QuotedStr(sdesa);
    active:=true;
    first;
    idesa:=FieldByName('TAMPIL').AsString;
    ikec:=FieldByName('KODE_KECAMATAN').AsString;
    ikabkot:=FieldByName('KODE_KABKOT').AsString;
    //
    active:=false;
    sql.Clear;
    sql.Text:='SELECT * FROM kecamatan WHERE KODE_KECAMATAN='+QuotedStr(ikec);
    active:=true;
    first;
    ikec:=FieldByName('TAMPIL').AsString;
    //
    active:=false;
    sql.Clear;
    sql.Text:='SELECT * FROM kabkot WHERE KODE='+QuotedStr(ikabkot);
    active:=true;
    first;
    ikabkot:=FieldByName('TAMPIL').AsString;
    //
    lblKet.Caption:=ikabkot+', Kec. '+ikec+', Desa./Kel. '+idesa;
  end;
  //TPS
  with ZQuery1 do begin
    active:=false;
    sql.Clear;
    sql.Text:='SELECT COUNT(*) AS HITUNG FROM daftar_tps WHERE KODE_DESA='+QuotedStr(sdesa)+'';
    Active:=true;
    first;
    ntps:=FieldByName('HITUNG').asstring;

    active:=false;
    sql.Clear;
    sql.Text:='SELECT MAX(NAMA+0) AS MAX FROM daftar_tps WHERE KODE_DESA='+QuotedStr(sdesa)+' AND NAMA NOT LIKE ''K%''';
    Active:=true;
    first;
    if (FieldByName('MAX').AsString='') then
      urut:=0
    else
      urut:=StrToInt(FieldByName('MAX').AsString);
//    ntps:=IntToStr(urut);
    lblTPS.Caption:=lblTPS.Caption+ntps+' TPS'; //cekNamaTPS(ntps);
  end;
  txtJml.Text:='';
  lblTambah.Caption:='';
  if(smode = 2) then begin
    txtjml.Visible:=false;
    lbltambah.Visible:=false;
    label1.Visible:=false;
    panel1.Visible := true;
  end;
end;

function TForm_TambahTPS.cekNamaTPS(kata:String) : String;
var
  stemp:String;
begin
  sTemp:='';
  if (Length(kata)=1) then
    sTemp:='00'+kata
  else if (Length(kata)=2) then
    sTemp:='0'+kata
  else if (Length(kata)>3) then
    sTemp:=Copy(kata, Length(kata)-3, 3)
  else
    sTemp:=kata;
  Result := sTemp;
end;

procedure TForm_TambahTPS.txtJmlChange(Sender: TObject);
var
  awal,ujung:Integer;
begin
  if (txtJml.Text<>'') then begin
    awal:=urut+1;
    ujung:=urut+StrToInt(txtJml.Text);
    lblTambah.Caption:='TPS yang ditambahkan adalah TPS '+cekNamaTPS(IntToStr(awal))+' - TPS '+cekNamaTPS(IntToStr(ujung));
  end;
end;

procedure TForm_TambahTPS.txtJmlKeyPress(Sender: TObject; var Key: Char);
begin
  KeyOnlyNum(Sender, Key);
end;

procedure TForm_TambahTPS.KeyOnlyNum(Sender: TObject; var Key: Char);
begin
  if not(Key in [#8, '0' .. '9']) then
  begin
    Key := #0;
  end;
end;

procedure TForm_TambahTPS.txtSatuanKeyPress(Sender: TObject;
  var Key: Char);
begin
  KeyOnlyNum(Sender, Key);
end;

end.
